/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the IncrementalParser package.
 *  
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.matrixparser.features;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class FeatureAggregatorScore extends AbstractFeatureAggregator {
    // General fields
    FeatureVector weights;
    double score = 0;

    // Optimum-related fields
    final static int MINIMUM = -1;
    final static int MAXIMUM = 1;
    final static int NONE = 0;
    int returnOptimum;
    double nextOptimalScore = 0;
    double nextAlternativeScore = 0;
    boolean nullAlternative = true;

    public FeatureAggregatorScore(FeatureVector weights, FeatureHandler featureHandler, int scorer) {
        super(featureHandler, scorer);
        this.weights = weights;
    }

    public void clear() {
        score = 0;
    }

    public double score() {
        return score;
    }

    public void addFeature(Feature feature, float weight) {
        if (returnOptimum != NONE) {
            nextAlternativeScore += weight * weights.getValue(feature);
            nullAlternative = false;
        } else {
            score += weight * weights.getValue(feature);
        }
    }

    public void addFeature(Feature feature) {
        if (returnOptimum != NONE) {
            nextAlternativeScore += weights.getValue(feature);
            nullAlternative = false;
        } else {
            score += weights.getValue(feature);
        }
    }

    // Optimal features
    public void openOptimalFeatureSequenceSpecification(boolean maximum) {
        returnOptimum = maximum ? 1 : -1;
        nextAlternativeScore = 0;
        nextOptimalScore = maximum ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
    }

    public boolean submitAlternativeFeatureSequence() {
        if (nullAlternative)
            return false;
        nullAlternative = true;
        if (returnOptimum == MAXIMUM) {
            if (nextAlternativeScore > nextOptimalScore) {
                nextOptimalScore = nextAlternativeScore;
                nextAlternativeScore = 0;
                return true;
            }
        } else if (returnOptimum == MINIMUM) {
            if (nextAlternativeScore < nextOptimalScore) {
                nextOptimalScore = nextAlternativeScore;
                nextAlternativeScore = 0;
                return true;
            }
        } else {
            // Do nothing: illegal call
        }
        nextAlternativeScore = 0;
        return false;
    }

    public void closeOptimalFeatureSequenceSpecification() {
        if (! nullAlternative)
            submitAlternativeFeatureSequence();
        score += nextOptimalScore;
    }

    boolean hasPrefix(int[] array, int[] prefix) {
        // Check for prefix equality
        for (int i = prefix.length - 1; i >= 0; --i)
            if (array[i] != prefix[i])
                return false;
        return true;
    }

    public void trimToSize() {
    }
}
